<?php
/**
 * 《资金情况》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */

require_once(dirname(__FILE__) . '/FinanceUtil.class.php');

class Funds extends FinanceUtil {
	public $tbl = Constant::tbl_cw_funds;
	
	public $type = array(
		'-' => '请选择',
		'1' => '个人',
		'2' => '公司',

		'9' => '其它',
	);
	
	public $category = array(
		// '-' => '请选择',
		'1' => '活期',
		'2' => '理财',			
		'3' => '投资',
			
		'6' => '已开支票',
		'7' => '保证金',
		'8' => '待核查',

		'9' => '其它',
	);
	
	public $name = array(
		'-' => '请选择',
		'景荣深圳' => '景荣深圳',
		'景荣工业集团' => '景荣工业集团',
		'广东景荣' => '广东景荣',
		'鸿景投资' => '鸿景投资',
		'景荣控股' => '景荣控股',
		'滨海中国' => '滨海中国',

		'其它' => '其它',
	);
	
	public $bank = array(
		// '-' => '请选择',
		'1' => '现金',
		'2' => '招商银行',
		'3' => '中国工商银行',
		'4' => '中国民生银行',
		'5' => '中信银行',
		'6' => '汇丰银行',
		'7' => '北京银行',
		'8' => '招商证券',

		'9' => '其它',
	);
	
	public $manual = array(
		'-' => '请选择',
		'1' => '手动',
		'2' => '自动',

		'9' => '其它',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '正在确认',
		'3' => '已经确认',

		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'date' => '更新日期',
		'manual' => '更新方式',
		'currency' => '币种',
		'amount' => '金额',
		'rmb' => '人民币',
	
		'emp' => '负责人',
		'oper' => '录入员',

		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'bank' => '银行',
		'name' => '名称',
		'emp' => '负责人',
		'oper' => '录入员',
	
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('category_option', $this->category);
		$controller->assign_by_ref('manual_option', $this->manual);
		$controller->assign_by_ref('bank_option', $this->bank);
		
		$controller->assign_by_ref('name_option', $this->name);
		
		$controller->assign_by_ref('bank_json', json_encode($this->bank));
	}
	
	function tab($controller, $request) {		
		$data = array(			
			// '资金信息' => array('cc'=>'finance.php?do=MoneyMy.find', 'js'=>'1'),
			// '资金情况' => array('cc'=>'finance.php?do=Money.find', 'js'=>''),
			// '资金明细' => array('cc'=>'finance.php?do=MoneyItem.find', 'js'=>''),
			// '资金历史' => array('cc'=>'finance.php?do=MoneyHistory.find', 'js'=>''),
		
			'家庭财务' => array('cc'=>'finance.php?do=BorrowSun.find', 'js'=>''),
		
			'理财信息' => array('cc'=>'finance.php?do=Invest.find', 'js'=>''),
			'外币兑换' => array('cc'=>'finance.php?do=Convert.find', 'js'=>''),
			'汇率更新' => array('cc'=>'finance.php?do=Finance.exchange', 'js'=>''),
			
			'转帐帐号' => array('cc'=>'emp.php?do=BankMy.find', 'js'=>''),
			'银行帐号' => array('cc'=>'finance.php?do=Bank.find', 'js'=>'1'),
			'联行号' => array('cc'=>'info.php?do=BankCode.find', 'js'=>''),
		
			'统计分析' => array('cc'=>'finance.php?do=Money.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(			
			'资金情况' => array('cc'=>'finance.php?do=Money.group', 'js'=>''),						
			'理财信息' => array('cc'=>'finance.php?do=Invest.group', 'js'=>''),
			
			'外币兑换' => array('cc'=>'finance.php?do=Convert.group', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('status_id', array(2, 3));
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '负责人',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		// $controller->assign('t1', strtotime('-3 month'));
		// $controller->assign('pagesize_s', '100');
		
		$controller->display($request, 'finance/funds/find');
		return true;
	}
	
	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['2'];
		$row['orderby_x'] = $row['modifytime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}
		
		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'date') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['date'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {		
		$bank_s = $request['bank_s'];
		$name_s = $request['name_s'];
		$type_s = $request['type_s'];
		
		$amount1_s = $request['amount1_s'];
		$amount2_s = $request['amount2_s'];
				
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$manual_s = $request['manual_s'];
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'cashier') {
			$request['cashier_s'] = $request['emp_a_s'];
		}
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		$where .= $this->_where_x("bank", $request['bank_s']);
		$where .= $this->_where_x("category", $request['category_s']);
				
		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content LIKE '%" . $name_s . "%')";
		}
		/*
		if ($type_s == '1') {
			$where .= " AND amount > 0";
		} else if ($type_s == '2') {
			$where .= " AND amount < 0";
		}*/
		
		if (!empty($amount1_s)) {
			$where .= " AND rmb >= '" . $amount1_s . "'";
		}

		if (!empty($amount2_s)) {
			$where .= " AND rmb < '" . $amount2_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND date < '" . $date2_s . "'";
		}
		
		if (!empty($manual_s) && $manual_s <> '-') {
			$where .= " AND manual = '" . $manual_s . "'";
		}
		
		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		$emp = $_SESSION['id'];
		
		$p = array('1'=>'163', '2'=>'167', '3'=>'173');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
			
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}

		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];				
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'finance/funds/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function my($controller, $request) {

		return true;
	}
	
	function _item_blank($len = 5) {
		$ret = array();
		$i = 0;
		for ($i = 0; $i < $len; $i++) {
			// $row['date'] = date("Y-m-d");
			// $row['type'] = "9:其它";
			$row['amount'] = "0.00";
				
			$row['ok'] = true;
				
			$ret[] = $row;
		}
	
		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 day'));
		
		$row['item'] = json_encode($this->_item_blank(2));
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/funds/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$bank = $request['bank'];
		$name = addslashes($request['name']);
		
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		$this->_zjm_clean($zjm);
		
		$amount = str_replace(",", "", $request['amount']);
			
		$type = $request['type'];
		$category = $request['category'];
		
		$currency = $request['currency'];
		$rate = $request['rate'];		
		$rmb = $request['rmb'];
		
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, bank, name, zjm, type, category, currency, amount, rate, rmb, date, content, oper, createtime, modifytime) ";
		$fmt .= "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $bank, $name, $zjm, $type, $category, $currency, $amount, $rate, $rmb, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/funds/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$bank = $request['bank'];
		$name = addslashes($request['name']);
		
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		$this->_zjm_clean($zjm);
		
		$amount = str_replace(",", "", $request['amount']);
		
		$type = $request['type'];
		$category = $request['category'];
		
		$currency = $request['currency'];				
		$rate = $request['rate'];		
		$rmb = $request['rmb'];
		
		$date = $request['date'];
		$content = addslashes($request['content']);
		$status = $request['status'];
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$this->_history($id, $date, 1, $_SESSION['id']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', bank = '%s', name = '%s', zjm = '%s', type = '%s', category = '%s', currency = '%s', amount = '%s', rate = '%s', rmb = '%s', date = '%s', ";
		$fmt .= "content = '%s', manual = '1', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $bank, $name, $zjm, $type, $category, $currency, $amount, $rate, $rmb, $date, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/money/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/funds/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['exchange'] = $this->_exchange($row['currency']);
		$row['bank_t'] = $this->bank[$row['bank']];
		$row['manual_t'] = $this->manual[$row['manual']];
		$row['category_t'] = $this->category[$row['category']];
		
		$row['z_day'] = $this->_z($row['id'], $row['amount'], $row['date'], 1);
		
		$row['x_day'] = $this->_x($row['id'], $row['rmb'], $row['date'], 1);
		$row['x_week'] = $this->_x($row['id'], $row['rmb'], $row['date'], 7);
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _history_list($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/MoneyHistory.class.php');
		$aa = new MoneyHistory();
		
		return $aa->_list($id);
	}
	
	function _x($id, $amount, $date, $day) {
		require_once(dirname(__FILE__) . '/MoneyHistory.class.php');
		$aa = new MoneyHistory();
		
		return $aa->_x($id, $amount, $date, $day);
	}
	
	function _z($id, $amount, $date, $day) {
		require_once(dirname(__FILE__) . '/MoneyHistory.class.php');
		$aa = new MoneyHistory();
		
		return $aa->_z($id, $amount, $date, $day);
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		$emp = $_SESSION['id'];
		if ($row['oper'] == $emp || $row['emp'] == $emp) {
			return true;
		}

		return false;
	}
	
	function batch_tj($controller, $request) {
		$this->pretreat($controller);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'finance/money/batch_tj');
		return true;
	}
	
	function t($controller, $request) {
		$db = newdb($this->dsn_w);

		$account = '2';
		
		$file = "/home/sunxy/m03.csv";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = trim(fgets($fd));
			// echo $buf;
			$buf = str_replace(",", "", $buf);

			if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
				continue;
			}
			
			$a = preg_split("/;/", $buf);			
			$no = trim($a[0]);
			
			if (!preg_match("/^\d+$/", $no)) {
				continue;
			}
			
			if (!empty($a[1])) {
				$date = $a[1];
			}

			// print_r($a);
// echo $a[1] . "\n";					
			
			$this->_aa($account, $date, 'CNY', $a[3], $a[4], $a[5], $a[2]);
			$this->_aa($account, $date, 'HKD', $a[6], $a[7], $a[8], $a[2]);
			$this->_aa($account, $date, 'USD', $a[9], $a[10], $a[11], $a[2]);
			$this->_aa($account, $date, 'EUR', $a[12], $a[13], $a[14], $a[2]);
		}
		fclose($fd);
		
		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function _aa($account, $date, $currency, $income, $out, $balance, $name) {
		$income = floatval($income);
		$out = floatval($out);
		// echo sprintf("%s %s\n", $income, $out);
		if ($income == 0 && $out == 0) {
			return;
		}
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(account, date, currency, income, `out`, balance, name, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $account, $date, $currency, $income, $out, $balance, $name);
		echo $sql . "\n";
		$db->query($sql);
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-3 month'));
		
		$num = array(
			'1' => '按次数',
			'2' => '按金额',
			'3' => '按人民币',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'finance/money/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'currency');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if ($num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(amount) AS c FROM %s a ";
		} else if ($num_s == '3') {
			$fmt = "SELECT %s AS a, SUM(rmb) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(amount) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'currency') {
			$row['a'] = $this->currency[$row['a']];
		} elseif ($px == 'bank') {
			$row['a'] = $this->bank[$row['a']];
		} else {
		
			$row = $this->_name_t($row, $px);
		}

		return $row;
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email('163');
		print_r($bb);
		if (!$bb || count($bb) == 0) {
			// echo "222";
			return false;
		}

		$from = $this->email;
		$subject = '资金情况';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'modifytime DESC'),
			'手动更新'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d') AND manual = 1", 'b'=>'modifytime DESC'),
			'自动更新'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d') AND manual = 2", 'b'=>'modifytime DESC'),
			'确认信息'=>array('w' => "status = 3", 'b'=>'emp'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}

		$controller->clear_assign('result');
		
		$body = $this->_tj_html($controller, $request, 'finance/money/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function ajax_zjm($controller, $request) {
		
		$data = $this->_ajax_zjm($request);
		echo json_encode($data);
	}
	
	function _zjm_clean($s) {
		if (empty($s)) {
			return false;
		}
	
		$s = strtolower($s);
		for ($i = 1; $i <= strlen($s); $i++) {
			$q = substr($s, 0, $i);
			$key = sprintf("%s_%s_%s_%s", $this->zjm, $this->tbl, $q);
	
			debug($key);
			clean($key);
		}
	
		return true;
	}
	
	function _ajax_zjm($request) {
		$zjm = $request['q'];
		
		if (preg_match("/^(\d+)(.+)/", $q, $m)) {
			$status = $m[1];
			$zjm = $m[2];
		} else {
			$status = 3;
			$zjm = $q;
		}
	
		if (empty($zjm)) {
			debug("OK" . $zjm);
			return array();
		}
	
		// $key = $this->prefix . "ajax_zjm_" . $q;
		if ($status == 1) {
			$key = sprintf("%s_%s_%s", $this->tbl, $this->prefix, $zjm);
		} else {
			$key = sprintf("%s_%s_%s_%s", $this->tbl, $this->prefix, $status, $zjm);
		}
	
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT *, length(zjm) c FROM %s WHERE status = '%s' AND zjm LIKE '%s' ORDER BY c, id DESC";
		$sql = sprintf($fmt, $this->tbl, $status, "%" . $zjm . "%");
	
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			// $row['dept_t'] = Name::dept($row['dept']);
	
			$ret[] = $row;
		}
	
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}
	
		return $ret;
	}
	
	function z($controller, $request) {
		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);
	
			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
			echo ".";
		}
	
		logdate($controller, $request);
		return true;
	}
}
?>
